//
//  VisitorLoginView.swift
//  sinaweibo
//
//  Created by ma c on 16/3/7.
//  Copyright © 2016年 ower. All rights reserved.
//

import UIKit
import SnapKit

//声明协议
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //声明协议方法
    func userWillRegister()
    
    //登陆的协议方法
    func userWillLognin()
}

class VisitorLoginView: UIView {
    
    //声明代理对象 代理的声明 在OC中必须使用  weak／assagin 为了防止循环引用
    weak var visitorDelegate: VisitorLoginViewDelegate?
    
    
    //MARK: 外部调用的方法
    func setupInfoWith(title:String,imageName:String?){
        tipLabel.text = title
        if imageName != nil{
            //不是首页
            circleIcon.image = UIImage(named: imageName!)
            //隐藏
            largehome.hidden = true
            //设置不是渐隐图片
            bringSubviewToFront(circleIcon)
        }else{
            //是首页
            //让图片旋转
            startAnimation()
        }
        
    }
    //MARK: 未登录界面开始动画效果
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //重复次数
        anim.repeatCount = MAXFLOAT
        //动画时间
        anim.duration = 10
        
        anim.toValue = 2 * M_PI
        //默认就是移除动画
        //在动画结束 或者页面处于非活跃状态时 动画效果不移除图层
        anim.removedOnCompletion = false
        circleIcon.layer.addAnimation(anim, forKey: nil)
    }
    
    //   找到入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        //        访客视图的自定义
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI(){
        
        addSubview(circleIcon)
        addSubview(maskImage)
        addSubview(largehome)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        largehome.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.height.equalTo(40)
            make.width.equalTo(230)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
           
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        maskImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        let value: CGFloat = 240 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
       
        //添加按钮的点击事件
        loginBtn.addTarget(self, action: "loginBtndidClick", forControlEvents:.TouchUpInside )
        registerBtn.addTarget(self, action: "registerBtndidClick", forControlEvents: .TouchUpInside)
    }
    //MARK: 按钮的监听事件
  @objc private  func loginBtndidClick(){
        print(__FUNCTION__)
        visitorDelegate?.userWillLognin()
    }
  @objc private  func registerBtndidClick(){
        print(__FUNCTION__)
        
        visitorDelegate?.userWillRegister()
    }
    
    
    
    //MARK: 懒加载子视图
    //遮罩视图
    private lazy var maskImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //大房子
    private lazy var largehome: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //转圈图
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    private lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        l.sizeToFit()
        l.numberOfLines = 0
        l.textAlignment = .Center
        return l
    }()
    
    //登陆按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        //设置背景图片的拉伸约束
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        //设置背景图片
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.sizeToFit()
        
        return btn
    }()
    //注册按钮
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        //设置背景图片
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        btn.sizeToFit()
        
        return btn
    }()
    
    
    
    
}

























